/*
 * Copyright 2010 Aekold Helbrass
 * 
 * This file is part of RosterComposer.
 * 
 * RosterComposer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RosterComposer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RosterComposer.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.heresylabs.rostercomposer;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import org.heresylabs.rostercomposer.roster.RosterEntry;
import org.heresylabs.rostercomposer.units.Option;

/**
 *
 * @author Aekold Helbrass <Helbrass@gmail.com>
 */
abstract public class AbstractOptionsGroup extends OptionComponent {

    protected final OptionCheck thisOptionCheck;
    protected final GridBagConstraints constraints;

    public AbstractOptionsGroup(RosterEntry rosterEntry, Option option) {
        super(rosterEntry, option);
        super.setLayout(new GridBagLayout());
        super.setToolTipText(composeTooltip());
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.weighty = 0.5;
        thisOptionCheck = new OptionCheck(rosterEntry, getOption());
        thisOptionCheck.addOptionListener(new OptionListener() {

            @Override
            public void fireOptionChanged(Option option, boolean selected) {
                if (!selected) {
                    deselect();
                }
                else {
                    uncheckEmptyGroup();
                }
            }

        });
        super.add(thisOptionCheck, c);
        c.insets = new Insets(0, 22, 0, 0);
        c.gridy = 1;
        constraints = c;
    }

    protected void postConstrucionSetup() {
        super.setMaximumSize(new Dimension(super.getMaximumSize().width, super.getPreferredSize().height));
        checkEnabled();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        thisOptionCheck.setEnabled(enabled);
    }

    abstract protected void uncheckEmptyGroup();

}
